import * as Common from '../common.js'
import { Util } from "../util.js";

export class Bag
{
    constructor()
    {
        // 构造函数  
    }

    static bagUse(goods_id_str)
    {
        var user_id = $.cookie('user_id');
        console.log("玩家使用物品:" + goods_id_str);
        if (goods_id_str == "15")
        {
            Util.netSendAddGoods(goods_id_str, "-1");
            // function(res) {
            //     {
            //         var countsbase = Util.getKvFromCookie("tael");
            //         var randcounts = Util.generateRandomInteger(1, 100000);
            //         var newcounts = randcounts + parseInt(countsbase);

            //         //notify
            //         if (randcounts > 80000)
            //             MsgMgr.sendMsg("1000", "系统", Util.getKvFromCookie("name") + "运气太好了打开小银袋获得" + randcounts + "银两", 3, () =>
            //             {
            //                 MsgMgr.getMsgByType(3);
            //             });
            //         netUpdateUserByKv(user_id, "tael", newcounts, 0, function ()
            //         {
            //             //update local
            //             setKvFromCookieBag("15", getKvFromCookieBag("15") - 1);
            //             setKvFromCookieUser("tael", newcounts);
            //             Bag.updateBagView();

            //         });
            //     }
        } else if (goods_id_str == "5")
        {
            if (parseInt(getKvFromCookieBag("5")) > 0)
            {
                netSendAddGoods(user_id, "5", "-1", () =>
                {
                    var countsbase = getKvFromCookie("phystr");
                    var newcounts = 50 + parseInt(countsbase);
                    netUpdateUserByKv(user_id, "phystr", newcounts, 0, function ()
                    {
                        //update local
                        setKvFromCookieBag("5", getKvFromCookieBag("5") - 1);
                        setKvFromCookieUser("tael", newcounts);
                        //update view
                        Bag.updateBagView();

                    });
                });
            }

        }

    }
    static updateBagView()
    {
        $("#tbd").children().remove();
        var objlist = JSON.parse($.cookie("bag"));

        for (let index = 0; index < objlist.length; index++)
        {
            //tael and coin don't show
            if ((parseInt(objlist[index]["goods_id"]) !== 16 && parseInt(objlist[index]["goods_id"]) !== 17))
            {
                var node = $("<tr><td scope='row'>" + Util.getGoodsNameByGid(objlist[index]["goods_id"]) + "</td><td>" + objlist[index]["counts"] + "</td><td><button  id='bagUseBtn" + objlist[index]["goods_id"] + "'  " + (parseInt(objlist[index]["counts"]) == 0 ? 'disabled' : '') + ">使用</button></td> </tr>");
                $("#tbd").append(node);
                $("#bagUseBtn" + objlist[index]["goods_id"]).on('click', function ()
                {
                    Bag.bagUse(objlist[index]["goods_id"]);
                });
            }
        }



    }
    static updateBag()
    {
        var user_id = $.cookie('user_id');
        $.post(
            Common.UrlBase + Common.ApiEnums.UserMgr,
            {
                "action": "getUserGoodsList",
                "user_id": user_id
            },
            function (res)
            {
                console.log("updateBag", res);
                if (res == 1)
                {
                    console.log("没有物品")
                    $("#bagmodal").modal("show");
                    return;
                }
                const resobj = JSON.parse(res);
                console.log(resobj.data);
                $.cookie("bag", JSON.stringify(resobj.data), { expires: 7 });
                Bag.updateBagView();
                $("#bagmodal").modal("show");

            }
        );

    }
}


